class IUP_ZBOX
-- Creates a void container for composing elements in hidden layers with only 
-- one layer visible. It is a box that piles up the children it contains, 
-- only the one child is visible.
--
-- It does not have a native representation.
--
-- Zbox works by changing the VISIBLE attribute of its children, so if any of 
-- the grand children has its VISIBLE attribute directly defined then Zbox 
-- will NOT change its state.
--
-- IMPORTANT: Each element must have a name defined by "set_widget_name". 

inherit
	IUP_CONTAINER
	IUP_WIDGET_CUSTOM_ATTRIBUTES

insert
	IUP_WIDGET_EXPAND
   IUP_WIDGET_SIZE
	IUP_WIDGET_RASTERSIZE
	IUP_WIDGET_USERSIZE
	IUP_WIDGET_WID
	IUP_WIDGET_FONT
	IUP_WIDGET_CLIENTSIZE
	IUP_WIDGET_CLIENTOFFSET
	IUP_WIDGET_POSITION
	IUP_WIDGET_MAXMIN_SIZE
	IUP_WIDGET_CHILD
	IUP_WIDGET_NAME
	IUP_WIDGET_CHILDSIZEALL
	
create {ANY}
	zbox_empty,
	zbox
	
feature {ANY}

	zbox_empty
	   -- Create an empty zbox
		local
			p, a_zbox: POINTER
		do
			a_zbox := int_zbox_empty (p)
			set_widget(a_zbox)
		end
		
	zbox (col: ARRAY[IUP_WIDGET])
	   -- Create a new zbox containing the list of widgets
		local
         iterator: ITERATOR[IUP_WIDGET]; i: INTEGER; arg: NATIVE_ARRAY[POINTER]; s: IUP_WIDGET; a_zbox: POINTER
		do
			i := col.count
			arg := arg.calloc(i)
			iterator := col.new_iterator
			i := 0
			
			from
				iterator.start
         until
				iterator.is_off
         loop
            s := iterator.item
            arg.put(s.widget, i)
				iterator.next
            i := i + 1
         end

			a_zbox := int_zbox (arg.to_external)
			set_widget(a_zbox)
		end

	-- Attributes

	set_alignment (value: STRING)
		-- (non inheritable): Defines the alignment of the visible child. Possible
		-- values: "NORTH", "SOUTH", "WEST", "EAST", "NE", "SE", "NW", "SW",
		-- "ACENTER". Default: "NW".
		require
			is_valid: is_valid_alignment(value)
		do
			iup_open.set_attribute(Current, "ALIGNMENT", value)
		end
	
	get_alignment: STRING
		-- Return the value of the alignment.
		do
			Result := iup_open.get_attribute(Current, "ALIGNMENT")
		end

	set_value (name: STRING)
		-- (non inheritable): The visible child accessed by its name. The value 
		-- passed must be the name of one of the children contained in the zbox. 
		-- Use set_attribute_handle to associate a child to a name. When the 
		-- value is changed the selected child is made visible and all other 
		-- children are made invisible, regardless their previous visible state.
		do
			iup_open.set_attribute(Current, "VALUE", name)
		end
	
	get_value: STRING
		-- Return the name of the visible child.
		do
			Result := iup_open.get_attribute(Current, "VALUE")
		end

	set_value_widget (wgt: IUP_WIDGET)
		-- (non inheritable): The visible child accessed by its handle. The 
		-- value passed must be the handle of a child contained in the zbox. 
		-- When the zbox is created, the first element inserted is set as the 
		-- visible child.
		do
			iup_open.set_attribute_widget(Current, "VALUE_HANDLE", wgt)
		end

	get_value_widget: IUP_WIDGET
		-- Return the IUP_WIDGET that is visible.
		do
			Result := iup_open.get_attribute_widget(Current, "VALUE_HANDLE")
		end
		
	set_value_position (pos: INTEGER)
		-- (non inheritable): The visible child accessed by its position. The 
		-- value passed must be the index of a child contained in the zbox, 
		-- starting at 0. When the zbox is created, the first element inserted 
		-- is set as the visible child.
		require
			pos >= 0
		do
			iup_open.set_attribute(Current, "VALUEPOS", pos.to_string)
		end
	
	get_value_position: INTEGER
		-- Return the position of the visible child.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "VALUEPOS")
			Result := str.to_integer
		end

feature {}

	-- Internals
	
	int_zbox_empty (arguments: POINTER): POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupZbox"
         }"
      end
	
   int_zbox (arguments: POINTER): POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupZboxv"
         }"
      end

	-- Validations

	is_valid_alignment (value: STRING): BOOLEAN
		do
			if value.is_equal("NORTH") or
				value.is_equal("SOUTH") or
				value.is_equal("WEST") or
				value.is_equal("EAST") or
				value.is_equal("NE") or
				value.is_equal("SE") or
				value.is_equal("NW") or
				value.is_equal("SW") or
				value.is_equal("ACENTER") then
				Result := True
			else
				Result := False
			end
		end

end -- class IUP_ZBOX

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
